package com.steshov.trainer.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.steshov.trainer.R;
import com.steshov.trainer.dao.ExerciseDAO;
import com.steshov.trainer.utils.Adapters;
import com.steshov.trainer.utils.App;

public class ExerciseActivity extends Activity implements OnClickListener {

	Button toTasksButton;
	Button toMainButton;
	Button createExercise;
	Button removeExercise;
	ExerciseDAO exerciseDAO = new ExerciseDAO();
	EditText exerciseNameField;
	Spinner spiner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exercise);
		toTasksButton = (Button) findViewById(R.id.button3);
		toMainButton = (Button) findViewById(R.id.button2);
		toTasksButton.setOnClickListener(Adapters.toTasksPage());
		toMainButton.setOnClickListener(Adapters.toMainPage());
		createExercise = (Button) findViewById(R.id.button1);
		TextView stressLevelLabel = (TextView) findViewById(R.id.textView4);
		stressLevelLabel.setText("Stress: "+getIntent().getStringExtra("stress"));
		createExercise.setOnClickListener(this);
		removeExercise = (Button) findViewById(R.id.button4);
		removeExercise.setOnClickListener(this);
		exerciseNameField = (EditText) findViewById(R.id.editText1);
		initSpinner();
	}

	private void initSpinner() {
		spiner = (Spinner) findViewById(R.id.spinner1);
		String[] spinnerArray = exerciseDAO.getExercises(ExerciseActivity.this);
		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, spinnerArray);
		spinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiner.setAdapter(spinnerArrayAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_index, menu);
		App.initOptMenu(menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		if (v == createExercise) {
			String exercise = exerciseNameField.getText().toString().trim();
			if (!exercise.equals("")) {
				exerciseDAO.peristExercise(ExerciseActivity.this, exercise);
				exerciseNameField.setText("");
			} else {
				App.msg(ExerciseActivity.this, "Exercise name can't be empty");
			}

		} else if (v == removeExercise) {
			if (spiner.getSelectedItemPosition() != -1) {
				exerciseDAO.removeExercise(ExerciseActivity.this, spiner
						.getSelectedItem().toString());
			}

		}
		initSpinner();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		App.onOptionsItemSelectedThing(item, this);
		return super.onOptionsItemSelected(item);
	}
}
